package com.pm.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pm.common.core.domain.AjaxResult;
import com.pm.common.core.domain.entity.SysUser;
import com.pm.common.utils.SecurityUtils;
import com.pm.project.domain.ProjectList;
import com.pm.project.domain.ProjectManagerMember;
import com.pm.project.domain.vo.ProjectManagerMemberVO;
import com.pm.project.mapper.ProjectManagerMemberMapper;
import com.pm.project.service.ProjectManagerMemberService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ProjectManagerMemberServiceImpl implements ProjectManagerMemberService {


    @Resource
    ProjectManagerMemberMapper projectManagerMemberMapper;


    @Override
    public List<ProjectManagerMember> list(ProjectManagerMemberVO projectManagerMemberVO) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        QueryWrapper<ProjectManagerMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("manager_user_id",sysUser.getUserId());
        return projectManagerMemberMapper.selectList(queryWrapper);
    }

    @Override
    public AjaxResult save(List<ProjectManagerMember> list) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        //先删除
        QueryWrapper<ProjectManagerMember> del = new QueryWrapper<>();
        del.eq("manager_user_id",sysUser.getUserId());
        projectManagerMemberMapper.delete(del);

        if(!CollectionUtils.isEmpty(list)){
            for (ProjectManagerMember projectManagerMember : list) {
                projectManagerMember.setManagerUserId(sysUser.getUserId());
                projectManagerMemberMapper.insert(projectManagerMember);
            }
        }

        return AjaxResult.success("操作成功!");
    }
}
